package com.example.myadapt;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private String[] titles = {"西瓜","香蕉","火龙果","樱桃"};
    private String[] prices = {"2.5元/kg","1.9元/kg","30元/kg","45元/kg"};
    private int[] pics = {R.drawable.x,R.drawable.b,R.drawable.w,R.drawable.y};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.list_view);
        MyAdapt adapt = new MyAdapt();
        listView.setAdapter(adapt);
    }
    class MyAdapt extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view1 = View.inflate(MainActivity.this,R.layout.list_item,null);
            ImageView iv = view1.findViewById(R.id.tv_iv);
            TextView tv_title = view1.findViewById(R.id.tv_title);
            TextView tv_price = view1.findViewById(R.id.tv_price);
            tv_title.setText(titles[position]);
            tv_price.setText(prices[position]);
            iv.setImageResource(pics[position]);
            return view1;
        }
    }
}
